package com.itheima.test;

import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class SpringTest01 {

    @Test
    public void test(){
        //1 创建Spring核心对象
        ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
        //2 从容器中根据id值获取对象
        //UserService userService = (UserService) ac.getBean("userService");
        UserService userService = ac.getBean("userService", UserService.class);
        //3 调用对象的方法
        userService.save();
    }

    public void test02() throws SQLException {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = ac.getBean("dataSource", DataSource.class);
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }

    @Test
    public void test3(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = ac.getBean("userDao", UserDao.class);
        System.out.println(userDao.toString());
    }
}
